package com.guchenghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guchenghao.entity.Article;
import com.guchenghao.entity.Comment;
import com.guchenghao.mapper.ArticleMapper;
import com.guchenghao.service.ArticleService;
import com.guchenghao.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @User 20675
 * @Author gch
 * @Date 2025/3/26 22:09
 * @Description <br />
 * <p>
 * <br />
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    
    @Autowired
    private CommentService commentService;


    @Override
    public Page<Article> showArticles(Integer pageNum, Integer pageSize) {
        Page<Article> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("publish_date");
        return articleMapper.selectPage(page, wrapper);
    }

    @Override
    public Article showArticle(Integer articleId) {
        // 查询单片文章
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("article_id", articleId);
        return articleMapper.selectOne(articleQueryWrapper);
    }

    @Override
    public Article showArticleWithComment(Integer id, Integer commentPageNum, Integer commentPageSize) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();
        articleQueryWrapper.eq("article_id", id);
        Article article = articleMapper.selectOne(articleQueryWrapper);
        // 查询附带用户信息的评论
        Page<Comment> commentPage = commentService.commentWithUserDtoPage(id, commentPageNum, commentPageSize);
        // 添加到 article 实体类中
        article.setCommentPage(commentPage);
        return article;
    }
}
